<?php
/**
 * @package Joomla
 * @subpackage Jmonoslideshow
 * @copyright (C) 2010 - Matthieu BARBE - www.ccomca.com
 * @license GNU/GPL v2
 * 
 * Jmonoslideshow is a Joomla extension for Monoslideshow.
 *
 * Jmonoslideshow uses :
 * Monoslideshow (http://www.monoslideshow.com/)
 * Plupload (http://www.plupload.com/) by Moxicode Systems
 * json class by Michal Migurski, Matt Knapp, Brett Stimmerman
 * jquery a JavaScript Library (http://jquery.com/)
 * getid3 library (http://getid3.sourceforge.net/)
 * swfobject is an easy-to-use and standards-friendly method to embed Flash content, which utilizes one small JavaScript file (http://code.google.com/p/swfobject/)
 * Chained Selects jQuery Plugin (http://www.appelsiini.net/2010/jquery-chained-selects)
 *
 * Jmonoslideshow is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
 

class MediaHelper
{
	
	/**
	 * Checks if the file is an image
	 * @param string The filename
	 * @return boolean
	 */
	function isImage( $fileName )
	{
		static $imageTypes = 'xcf|odg|gif|jpg|png|bmp';
		return preg_match("/$imageTypes/i",$fileName);
	}
	
	/**
	 * Get an extension authorize
	 *
	 * @return string (extension)
	 */
	function extension_authorize ()
	{
		$params = &JComponentHelper::getParams( 'com_jmonoslideshow' );
		$ext = "jpg,png,jpeg,gif";
		if ($params->get( 'authorize_swf', 1) == 1) $ext .= ",swf";
		if ($params->get( 'authorize_video', 1) == 1) $ext .= ",f4v,flv";
		return $ext;
	}
	
	/**
	 * Get a type media
	 *
	 * @param string extension media
	 * @return int
	 */
	function getType ($extension)
	{
		if ($extension == "swf")
			{
				$type = 1;
			}
			else if ($extension == "f4v" || $extension == "flv")
			{
				$type = 2;
			}
			else
			{
				$type = 0;
			}
			
			return $type;
	}
	
	/**
	 * Get a extension file
	 *
	 * @param string extension media
	 * @return string
	 */
	function getExtension ($extension)
	{
		$type = MediaHelper::getType($extension);
		if ($type == 1)
			{
				$extension = "swf";
			}
			else if ($type == 2)
			{
				$extension = $extension;
			}
			else
			{
				$extension = $extension;
			}
			
			return $extension;
	}
	
	/**
	 * Get a type name
	 *
	 * @param int type
	 * @return string
	 */
	function getTypeName ($type)
	{
		
		if ($type == 1)
			{
				$name = "flash";
			}
			else if ($type == 2)
			{
				$name = "video";
			}
			else
			{
				$name = "image";
			}
			
			return $name;
	}
	
	/**
	 * Get a info media
	 *
	 * @param int type
	 * @param int id
	 * @return string
	 */
	function getInfoMedia ($type, $id, $extension)
	{
		
		if ($type == 1 || $type == 2)
			{
				$info = "";
			}
			else
			{
				$params = &JComponentHelper::getParams( 'com_jmonoslideshow' );
				$info = "<img src='".JURI::root()."images/monoslideshow/images/".$id.".".$extension."' width='".$params->get( 'width_image_hover', 400 )."' />";
			}
			
			return $info;
	}


	function countFiles( $dir )
	{
		$total_file = 0;
		$total_dir = 0;

		if (is_dir($dir)) {
			$d = dir($dir);

			while (false !== ($entry = $d->read())) {
				if (substr($entry, 0, 1) != '.' && is_file($dir . DIRECTORY_SEPARATOR . $entry) && strpos($entry, '.html') === false && strpos($entry, '.php') === false) {
					$total_file++;
				}
				if (substr($entry, 0, 1) != '.' && is_dir($dir . DIRECTORY_SEPARATOR . $entry)) {
					$total_dir++;
				}
			}

			$d->close();
		}

		return array ( $total_file, $total_dir );
	}
	
	/**
	 * Get a info media ID tag
	 *
	 * @param int type
	 * @param int id
	 * @param extension string
	 * @return string
	 */
	
	function info_media ($type, $file)
	{
		require_once JPATH_COMPONENT_ADMINISTRATOR.DS.'library'.DS.'getid3'.DS.'getid3.php';
		
		// Needed for windows only
		define('GETID3_HELPERAPPSDIR', 'C:/helperapps/');
		$getID3 = new getID3;
		$getID3->setOption(array('encoding' => 'UTF-8'));
		$file = JPATH_ROOT."".$file;
		
		if (GETID3_OS_ISWINDOWS) {
		
		// this mostly just gives a consistant look to Windows and *nix filesystems
		// (windows uses \ as directory seperator, *nix uses /)
		$file = str_replace('\\', '/', $file);
		}
		
		
		$ThisFileInfo = $getID3->analyze($file);
		getid3_lib::CopyTagsToComments($ThisFileInfo);
		//print_r ($ThisFileInfo);
		return @$ThisFileInfo['playtime_string'];
	}

}